from fastapi import APIRouter, Depends, Security
from fastapi.responses import JSONResponse

from myProfile.models.data.myProfile_models import Login
from myProfile.repository.profile import ProfileRepository
from myProfile.models.request.myProfile_request import ProfileReq
from myProfile.db_config.odmantic_config import create_db_engine
from myProfile.security.secure import get_current_valid_user

from datetime import date, datetime
from json import dumps, loads

router = APIRouter()


def json_serial(obj):
    if isinstance(obj, (datetime, date)):
        return obj.strftime('%Y-%m-%dT%H:%M:%S.%f')
    raise TypeError("The type %s not serializable." % type(obj))


@router.post("/profiles/add")
async def add_profiles(req: ProfileReq,
                       current_user: Login = Security(
                           get_current_valid_user, scopes=["user"]),
                       engine=Depends(create_db_engine)):
    profile_dict = req.model_dump(exclude_unset=True)
    profile_json = dumps(profile_dict, default=json_serial)
    repo: ProfileRepository = ProfileRepository(engine)
    result = await repo.insert_profile(current_user.id, loads(profile_json))
    if result is True:
        return req
    else:
        return JSONResponse(
            content={"message": "add profiles problem encountered."},
            status_code=500)


@router.patch("/profiles/update")
async def update_profiles(req: ProfileReq,
                          current_user: Login = Security(
                              get_current_valid_user, scopes=["user"]),
                          engine=Depends(create_db_engine)):
    profile_dict = req.model_dump(exclude_unset=True)
    profile_json = dumps(profile_dict, default=json_serial)
    repo: ProfileRepository = ProfileRepository(engine)
    result = await repo.update_profile(current_user.id, loads(profile_json))
    if result is True:
        return req
    else:
        return JSONResponse(
            content={"message": "update profiles unsuccessfully"},
            status_code=500)


@router.delete("/profiles/delete")
async def delete_profiles(current_user: Login = Security(
                            get_current_valid_user, scopes=["user"]),
                          engine=Depends(create_db_engine)):
    repo: ProfileRepository = ProfileRepository(engine)
    result = await repo.delete_profile(current_user.id)
    if result is True:
        return JSONResponse(
            content={"message": "delete profiles successfully"},
            status_code=201)
    else:
        return JSONResponse(
            content={"message": "delete profiles unsuccessfully"},
            status_code=500)


@router.get("/profiles/get", response_model=ProfileReq)
async def get_profiles(current_user: Login = Security(
                            get_current_valid_user, scopes=["user"]),
                       engine=Depends(create_db_engine)):
    repo: ProfileRepository = ProfileRepository(engine)
    return await repo.get_profile(current_user.id)
